home *** CD-ROM | disk | FTP | other *** search
- #include "AppConstants.h"
- #include "AppMacros.h"
- #include "AppTypes.h"
- #include "AppGlobals.h"
- #include "GridCopyPr.h"
- #include "NumberStringUtilPr.h"
- #include "ToolTrapsPr.h"
-
- #include "DrawGridUtilPr.h"
-
- static void drawHexCell ( register unsigned char *, register long );
- static void eraseHexCell ( register unsigned char *, register long );
- static void drawHexCellMini ( register unsigned char *, register long );
- static void eraseHexCellMini ( register unsigned char *, register long );
- static void drawHexCellTiny (register unsigned char *, register long );
- static void eraseHexCellTiny ( register unsigned char *, register long );
-
- // drawHexGrid - call to draw the entire hex grid of the given window
- void drawHexGrid( WindowRef theWindow )
- {
- register GridDataPtr refCon = (GridDataPtr)MGetWRefCon( theWindow );
- register unsigned char **position = refCon->position;
- register long count, total = refCon->gridTotalSize;
- register Boolean *oldGrid = refCon->oldGrid;
- CGrafPtr saveCPort = nil;
- GDHandle saveGDevice = nil;
- register long rowLength = refCon->rowBytes;
-
- GetGWorld( &saveCPort, &saveGDevice );
- SetGWorld( refCon->theGWorld, nil );
- MEraseRect( &refCon->theGWorld->portRect );
-
- // draw the grid - depends on which hexagon size of being used
- if( refCon->scale == kHexSize )
- {
- // 16x16
- for( count = 0; count < total; count++, position++ )
- if( *oldGrid++ )
- drawHexCell( *position, rowLength );
- }
- else if( refCon->scale == kMiniHexSize )
- {
- // 8x8
- for( count = 0; count < total; count++, position++ )
- if( *oldGrid++ )
- drawHexCellMini( *position, rowLength );
- }
- else
- {
- // 4x4
- for( count = 0; count < total; count++, position++ )
- if( *oldGrid++ )
- drawHexCellTiny( *position, rowLength );
- }
-
- // draw the iteration number
- if( refCon->drawIteration )
- {
- Rect theRect;
-
- MSetRect( theRect, 5, 3, 100, 13 );
- MEraseRect( &theRect );
- MMoveTo( 5, 12 );
- MDrawString( "\pIteration: " );
- MDrawString( refCon->iterationString );
- }
-
- SetGWorld( saveCPort, saveGDevice );
- }
-
- // calculateNextIteration - call to calculate the next iteration
- void calculateNextIteration( WindowRef theWindow )
- {
- GridDataPtr refCon = (GridDataPtr)MGetWRefCon( theWindow );
- unsigned char **position = refCon->position;
- register long count, total = refCon->gridTotalSize;
- register Boolean *newGrid = refCon->newGrid, *oldGrid = refCon->oldGrid;
- Boolean *alive = refCon->alive, *dead = refCon->dead;
- register NeighbourPtr neighbourPtr = refCon->neighbours;
- register long rowLength = refCon->rowBytes;
-
- // calculate the new grid - drawing depends on which scale is being used
- if( refCon->scale == kHexSize )
- {
- // calculate the next iteration and draw using normal hex cells (16x16)
- for( count = 0; count < total; count++, position++, neighbourPtr++ )
- {
- register unsigned char neighbours = *(neighbourPtr->neighbour[0]);
-
- // get the total number of alive neighbours
- neighbours += *(neighbourPtr->neighbour[1]);
- neighbours += *(neighbourPtr->neighbour[2]);
- neighbours += *(neighbourPtr->neighbour[3]);
- neighbours += *(neighbourPtr->neighbour[4]);
- neighbours += *(neighbourPtr->neighbour[5]);
-
- // depending on the cell's old state, calculate the new state depending on the number of neighbours
- if( *oldGrid++ )
- {
- if( !(*newGrid++ = alive[neighbours]) )
- eraseHexCell( *position, rowLength );
- }
- else
- if( *newGrid++ = dead[neighbours] )
- drawHexCell( *position, rowLength );
- }
- }
- else if( refCon->scale == kMiniHexSize )
- {
- // calculate the next iteration and draw using mini hex cells (8x8)
- for( count = 0; count < total; count++, position++, neighbourPtr++ )
- {
- register unsigned char neighbours = *(neighbourPtr->neighbour[0]);
-
- // get the total number of alive neighbours
- neighbours += *(neighbourPtr->neighbour[1]);
- neighbours += *(neighbourPtr->neighbour[2]);
- neighbours += *(neighbourPtr->neighbour[3]);
- neighbours += *(neighbourPtr->neighbour[4]);
- neighbours += *(neighbourPtr->neighbour[5]);
-
- // depending on the cell's old state, calculate the new state depending on the number of neighbours
- if( *oldGrid++ )
- {
- if( !(*newGrid++ = alive[neighbours]) )
- eraseHexCellMini( *position, rowLength );
- }
- else
- if( *newGrid++ = dead[neighbours] )
- drawHexCellMini( *position, rowLength );
- }
- }
- else
- {
- // calculate the next iteration and draw using tiny hex cells (4x4)
- for( count = 0; count < total; count++, position++, neighbourPtr++ )
- {
- register unsigned char neighbours = *(neighbourPtr->neighbour[0]);
-
- // get the total number of alive neighbours
- neighbours += *(neighbourPtr->neighbour[1]);
- neighbours += *(neighbourPtr->neighbour[2]);
- neighbours += *(neighbourPtr->neighbour[3]);
- neighbours += *(neighbourPtr->neighbour[4]);
- neighbours += *(neighbourPtr->neighbour[5]);
-
- // depending on the cell's old state, calculate the new state depending on the number of neighbours
- if( *oldGrid++ )
- {
- if( !(*newGrid++ = alive[neighbours]) )
- eraseHexCellTiny( *position, rowLength );
- }
- else
- if( *newGrid++ = dead[neighbours] )
- drawHexCellTiny( *position, rowLength );
- }
- }
-
- // draw the iteration number
- if( refCon->drawIteration )
- {
- Rect theRect;
- CGrafPtr saveCPort = nil;
- GDHandle saveGDevice = nil;
-
- GetGWorld( &saveCPort, &saveGDevice );
- SetGWorld( refCon->theGWorld, nil );
- MSetRect( theRect, 5, 3, 100, 13 );
- MEraseRect( &theRect );
- MMoveTo( 5, 12 );
- Add1ToString( refCon->iterationString );
- MDrawString( "\pIteration: " );
- MDrawString( refCon->iterationString );
- SetGWorld( saveCPort, saveGDevice );
- }
-
- // copy the new grid over the old grid
- RBlockMove( (Ptr)refCon->newGrid, (Ptr)refCon->oldGrid, refCon->gridMove );
- }
-
- // drawHexCell - call to draw a 16x16 hexagonal cell in the given PixMap
- static void drawHexCell( register unsigned char *startAddr, register long rowLength )
- {
- *((long *)(startAddr + 6)) = kTileEndLong;
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileEndLong;
- //#else
- // *((double *)(startAddr + 4)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)(startAddr + 2)) = kTileColorLong;
- *((long *)(startAddr + 6)) = kTileColorLong;
- *((long *)(startAddr + 10)) = kTileEndLong;
- //#else
- // *((double *)(startAddr + 2)) = kTileColorDouble;
- // *((long *)(startAddr + 10)) = kTileEndLong;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileColorLong;
- *((long *)(startAddr + 12)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileColorDouble;
- // *((double *)(startAddr + 8)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileColorLong;
- *((long *)(startAddr + 12)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileColorDouble;
- // *((double *)(startAddr + 8)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileColorLong;
- *((long *)(startAddr + 12)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileColorDouble;
- // *((double *)(startAddr + 8)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileColorLong;
- *((long *)(startAddr + 12)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileColorDouble;
- // *((double *)(startAddr + 8)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileColorLong;
- *((long *)(startAddr + 12)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileColorDouble;
- // *((double *)(startAddr + 8)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileColorLong;
- *((long *)(startAddr + 12)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileColorDouble;
- // *((double *)(startAddr + 8)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileColorLong;
- *((long *)(startAddr + 12)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileColorDouble;
- // *((double *)(startAddr + 8)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileColorLong;
- *((long *)(startAddr + 12)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileColorDouble;
- // *((double *)(startAddr + 8)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileColorLong;
- *((long *)(startAddr + 12)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileColorDouble;
- // *((double *)(startAddr + 8)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileColorLong;
- *((long *)(startAddr + 12)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileColorDouble;
- // *((double *)(startAddr + 8)) = kTileEndDouble;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)(startAddr + 2)) = kTileColorLong;
- *((long *)(startAddr + 6)) = kTileColorLong;
- *((long *)(startAddr + 10)) = kTileEndLong;
- //#else
- // *((double *)(startAddr + 2)) = kTileColorDouble;
- // *((long *)(startAddr + 10)) = kTileEndLong;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)(startAddr + 4)) = kTileColorLong;
- *((long *)(startAddr + 8)) = kTileEndLong;
- //#else
- // *((double *)(startAddr + 4)) = kTileEndDouble;
- //#endif
- *((long *)(startAddr + rowLength + 6)) = kTileEndLong;
- }
-
- // eraseHexCell - call to erase a 16x16 hexagonal cell in the given PixMap
- static void eraseHexCell( register unsigned char *startAddr, register long rowLength )
- {
- *((long *)(startAddr + 6)) = 0;
- startAddr += rowLength;
- #ifndef powerc
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- #else
- *((double *)(startAddr + 4)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)(startAddr + 2)) = 0;
- *((long *)(startAddr + 6)) = 0;
- *((long *)(startAddr + 10)) = 0;
- #else
- *((double *)(startAddr + 2)) = 0;
- *((long *)(startAddr + 10)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- *((long *)(startAddr + 12)) = 0;
- #else
- *((double *)startAddr) = 0;
- *((double *)(startAddr + 8)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- *((long *)(startAddr + 12)) = 0;
- #else
- *((double *)startAddr) = 0;
- *((double *)(startAddr + 8)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- *((long *)(startAddr + 12)) = 0;
- #else
- *((double *)startAddr) = 0;
- *((double *)(startAddr + 8)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- *((long *)(startAddr + 12)) = 0;
- #else
- *((double *)startAddr) = 0;
- *((double *)(startAddr + 8)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- *((long *)(startAddr + 12)) = 0;
- #else
- *((double *)startAddr) = 0;
- *((double *)(startAddr + 8)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- *((long *)(startAddr + 12)) = 0;
- #else
- *((double *)startAddr) = 0;
- *((double *)(startAddr + 8)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- *((long *)(startAddr + 12)) = 0;
- #else
- *((double *)startAddr) = 0;
- *((double *)(startAddr + 8)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- *((long *)(startAddr + 12)) = 0;
- #else
- *((double *)startAddr) = 0;
- *((double *)(startAddr + 8)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- *((long *)(startAddr + 12)) = 0;
- #else
- *((double *)startAddr) = 0;
- *((double *)(startAddr + 8)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- *((long *)(startAddr + 12)) = 0;
- #else
- *((double *)startAddr) = 0;
- *((double *)(startAddr + 8)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)(startAddr + 2)) = 0;
- *((long *)(startAddr + 6)) = 0;
- *((long *)(startAddr + 10)) = 0;
- #else
- *((double *)(startAddr + 2)) = 0;
- *((long *)(startAddr + 10)) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)(startAddr + 4)) = 0;
- *((long *)(startAddr + 8)) = 0;
- #else
- *((double *)(startAddr + 4)) = 0;
- #endif
- *((long *)(startAddr + rowLength + 6)) = 0;
- }
-
- // drawHexCellMini - call to draw a 8x8 mini hexagonal cell in the given PixMap
- static void drawHexCellMini( register unsigned char *startAddr, register long rowLength )
- {
- *(startAddr + 3) = kTileColorChar;
- startAddr += rowLength;
- *((long *)(startAddr + 1)) = kTileColorLong;
- *(startAddr + 5) = kTileColorChar;
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileEndLong;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileEndLong;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileEndLong;
- //#endif
- startAddr += rowLength;
- //#ifndef powerc
- *((long *)startAddr) = kTileColorLong;
- *((long *)(startAddr + 4)) = kTileEndLong;
- //#else
- // *((double *)startAddr) = kTileEndLong;
- //#endif
- startAddr += rowLength;
- *((long *)(startAddr + 1)) = kTileColorLong;
- *(startAddr + 5) = kTileColorChar;
- startAddr += rowLength;
- *(startAddr + 3) = kTileColorChar;
- }
-
- // eraseHexCellMini - call to erase a 8x8 mini hexagonal cell in the given PixMap
- static void eraseHexCellMini( register unsigned char *startAddr, register long rowLength )
- {
- *(startAddr + 3) = 0;
- startAddr += rowLength;
- *((long *)(startAddr + 1)) = 0;
- *(startAddr + 5) = 0;
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- #else
- *((double *)startAddr) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- #else
- *((double *)startAddr) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- #else
- *((double *)startAddr) = 0;
- #endif
- startAddr += rowLength;
- #ifndef powerc
- *((long *)startAddr) = 0;
- *((long *)(startAddr + 4)) = 0;
- #else
- *((double *)startAddr) = 0;
- #endif
- startAddr += rowLength;
- *((long *)(startAddr + 1)) = 0;
- *(startAddr + 5) = 0;
- startAddr += rowLength;
- *(startAddr + 3) = 0;
- }
-
- // drawHexCellTiny - call to draw a 4x4 tiny hexagonal cell in the given PixMap
- static void drawHexCellTiny( register unsigned char *startAddr, register long rowLength )
- {
- *((short *)(startAddr + 1)) = kTileColorShort;
- startAddr += rowLength;
- *((long *)startAddr) = kTileColorLong;
- startAddr += rowLength;
- *((long *)startAddr) = kTileColorLong;
- startAddr += rowLength;
- *((short *)(startAddr + 1)) = kTileColorShort;
- }
-
- // eraseHexCellTiny - call to erase a 4x4 tiny hexagonal cell in the given PixMap
- static void eraseHexCellTiny( register unsigned char *startAddr, register long rowLength )
- {
- *((short *)(startAddr + 1)) = 0;
- startAddr += rowLength;
- *((long *)startAddr) = 0;
- startAddr += rowLength;
- *((long *)startAddr) = 0;
- startAddr += rowLength;
- *((short *)(startAddr + 1)) = 0;
- }
-